package com.xh.audit.controller;

import com.mybatisflex.core.paginate.Page;
import com.xh.audit.entity.WorkPlan;
import com.xh.audit.service.WorkPlanService;
import com.xh.audit.vo.webform.WorkPlanQuery;
import com.xh.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 审计工作计划;一个计划多个项目 ,一个项目只有一个计划， 先有计划再到项目 ****重要****
 * (参照wps文件版本 保存调整记录) 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/work-plan")
@RequiredArgsConstructor
@Validated
public class WorkPlanController {
    private final WorkPlanService workPlanService;

    @PostMapping("/save")
    public R<WorkPlan> save(@RequestBody
                            @Validated
                                    WorkPlan tosave) {
        boolean saveFlag = workPlanService.saveWorkPlan(tosave);

        if (saveFlag) {
            return R.success(workPlanService.getById(tosave.getId()));
        }
        return R.failed("保存失败");
    }

    @PostMapping("/queryByPage")
    public R<Page<WorkPlan>> query(@RequestBody
                                   @Validated
                                           WorkPlanQuery queryPage) {

        Page<WorkPlan> page1 = workPlanService.page(queryPage.getPageInfo(), queryPage.queryWrapperBuild());
        return R.success(page1);
    }

    @PostMapping("/info/{id}")
    public R<WorkPlan> info(@PathVariable
                            @Validated
                            @NotNull(message = "id不能空")
                                    Integer id) {

        WorkPlan byId = workPlanService.getById(id);
        return R.success(byId);
    }


    /**
     * List<Integer> 集合验证 必须在类上也加 @Validated
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public R<WorkPlan> del(@RequestBody
                           @Validated
                           @NotEmpty(message = "ids 集合不能空")
                                   List<Integer> ids) {
        workPlanService.delWorkPlans(ids);
        return R.success("删除成功");
    }

}
